package com.sskj.mqtt.many;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Like
 * @Date: 2025/3/3 14:51
 * @Description:
 */
@RestController
@RequestMapping("/mqttMany")
@Slf4j
public class MqttManyController {

    @GetMapping("/sendMsg")
    public String sendMsg(String id, String topic, String msg) throws MqttException {
        MqttClientTemplate.publish(id, topic, msg);

        return "成功";
    }

    @MqttListener(clientId = "client1", topic = "/test/+")
    public void ListenerMsg1(String mqttClientId, String topic, byte[] payload) {
        String data = new String(payload);
        log.info("收到来自 {}===={} 的消息：{}", mqttClientId, topic, data);
    }

    @MqttListener(clientId = "client2", topic = "/test/#")
    public void ListenerMsg2(String mqttClientId, String topic, byte[] payload) {
        String data = new String(payload);
        log.info("收到来自 {}===={} 的消息：{}", mqttClientId, topic, data);
    }


}
